#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：2.使用csv写入字典数据.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-29 10:31
'''
import csv


rows = [
    {
        "class_name": "18级Python",
        "name": '小王',
        "gender": '男',
        "phone": '13146060xx1',
        "qq": '123456xx1'
    },
    {
        "class_name": "18级Python",
        "name": '小李',
        "gender": '男',
        "phone": '13146060xx2',
        "qq": '123456xx2'
    },
    {
        "class_name": "19级Python",
        "name": '小赵',
        "gender": '女',
        "phone": '13146060xx3',
        "qq": '123456xx3'
    },
    {
        "class_name": "19级Python",
        "name": '小红',
        "gender": '女',
        "phone": '13146060xx4',
        "qq": '123456xx4'
    },
]

# 表头得是字典得key值
headers = ['class_name', 'name', 'gender', 'phone', 'qq']

with open("2test.csv", 'w', newline='') as f:
    # 创建字典写入对象
    fcsv = csv.DictWriter(f, headers)

    # 将传递的表头写入
    fcsv.writeheader()

    # 写入数据
    fcsv.writerows(rows)